package com.shm.client.login;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.shm.client.R;
import com.shm.client.SHM_ClientApplication;

public class LoginActivity extends Activity {
	
	private EditText id_Et;
	private EditText pwd_Et;
	
	private Button login_Btn;
	
	private CheckBox saveAccount_Cb;
	
	private SHM_ClientApplication application;
	
	private boolean isSaveAccount = false;

	@Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.d("shm", "LoginActivity Start");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        application = (SHM_ClientApplication)getApplication();
        
        id_Et = (EditText)findViewById(R.id.id_Et);
        pwd_Et = (EditText)findViewById(R.id.pwd_Et);
        
        login_Btn = (Button)findViewById(R.id.login_Btn);
        login_Btn.setText(R.string.login);
        login_Btn.setOnClickListener(new OnClickListener() {
			//Log-in
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isSaveAccount)
					application.setAccount(id_Et.getText().toString(), pwd_Et.getText().toString());
					
				application.login(id_Et.getText().toString(), pwd_Et.getText().toString());
			}
		});
        
        saveAccount_Cb = (CheckBox)findViewById(R.id.saveAccount_Cb);
        saveAccount_Cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
					isSaveAccount = true;
				else
					isSaveAccount = false;
			}
		});
        
        checkAccount();
        
	}
	
	private void checkAccount() {
		if(application.checkAccountSaved()) {
			String[] account = application.getAccount();
			id_Et.setText(account[0]);
			pwd_Et.setText(account[1]);
		}
	}
}
